﻿
using BuyWithoutLoseMoney.Core.Models;
using BuyWithoutLoseMoney.Core.Repositories;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace BuyWithoutLose.Controllers
{
    [AllowAnonymous]
    public class ProductsController : Controller
    {
        private readonly ILogger<ProductsController> _logger;
        private readonly IRepository<Product, int> _productRepostitory;
        private readonly IRepository<ProductType, int> _productTypeRepostitory;
        private readonly IRepository<ProductPic, int> _productPicRepostitory;
        private readonly IRepository<Comment, int> _commentRepostitory;
        private readonly IRepository<User, int> _userRepostitory;
        private readonly IRepository<ResPosition, int> _resPositionRepostitory;

        public ProductsController(ILogger<ProductsController> logger,
           IRepository<Product, int> productRepository,
           IRepository<ProductType, int> productTypeRepository,
           IRepository<ProductPic, int> productPicRepostitory,
           IRepository<Comment, int> commentRepostitory,
           IRepository<User, int> userRepostitory,
            IRepository<ResPosition, int> resPositionRepostitory
            )
        {
            _logger = logger;
            _productRepostitory = productRepository;
            _productTypeRepostitory = productTypeRepository;
            _productPicRepostitory = productPicRepostitory;
            _commentRepostitory = commentRepostitory;
            _userRepostitory= userRepostitory;
            _resPositionRepostitory= resPositionRepostitory;
        }
        #region 商品
        /// <summary>
        /// 将所有的产品显示到页面中
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> Index()
        {
            var pro = await _productRepostitory.GetAllListAsync();
            var proPic = await _productPicRepostitory.GetAllListAsync();
            ViewBag.ProPic = proPic;
            var posPro = await _resPositionRepostitory.GetAllListAsync();
            ViewBag.PosPro = posPro;
            var proType = await _productTypeRepostitory.GetAllListAsync();
            ViewBag.ProType = proType;
            return View(pro);
        }
        #endregion

        #region 商品详情
        /// <summary>
        /// 当前商品的商品详情页面
        /// </summary>
        /// <param name="id">当前商品的ID</param>
        /// <returns></returns>
        public IActionResult ProDetails(int id)
        {
            if (HttpContext.User.Identity.IsAuthenticated)//判断是否登录
            {
                //获取前台用户登录ID
                var userName = HttpContext.User.Claims.First().ValueType;
                var userid = int.Parse(userName);
                var user = _userRepostitory.GetAll().Where(x => x.Id == userid).FirstOrDefault();//获取当前用户的信息
                ViewBag.User = user;
                var proPic = _productPicRepostitory.GetAllList();
                ViewBag.ProPic = proPic;
                var comment = _commentRepostitory.GetAllList();
                ViewBag.Comment = comment;
                var pro = _productRepostitory.GetAllList().Where(x => x.Id == id).FirstOrDefault();
                return View(pro);
            }
            else
            {
                return Redirect("/login/Login");
            }

        }
        [HttpPost]
        public async Task<IActionResult> ProDetails(Comment comment)
        {
            comment.CreateTime = DateTime.Now;
            if (ModelState.IsValid)
            {
                await _commentRepostitory.InsertAsync(comment);
                return Json(new { isSuccess = true, msg = "添加成功，页面即将刷新！" });
            }
            return Json(new { isSuccess = false, msg = "添加失败，未设置用户昵称！" });
        }
        #endregion

    }
}
